const express = require('express')
const expressWs = require("express-ws")
const app = express()
const fs=require('fs')
expressWs(app)

let json=fs.readFileSync('./7CVE.json').toString()
let websocket=null

app.ws("/ws", function (ws, req) {
    console.log('connected')
    websocket=ws
    ws.on("message", function (str) {
        console.log(str)
        let data=JSON.parse(str)
        switch (data.type) {
            case 'update_graph':
                if (fs.existsSync('./' + data.payload + '.json')) json = fs.readFileSync('./' + data.payload + '.json').toString()
                send({
                    type:data.type,
                    payload:json
                })
                break
            case 'message':
                console.log(data.payload)
                break

        }
    })
});

app.listen(3000)

function send(json) {
    let str=JSON.stringify(json)
    // console.log('send'+str)
    websocket.send(str)
}